package com.rulin.asp.business.externalservice.uploadapi.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rulin.asp.business.entity.Question;
import com.rulin.asp.business.errorcorrection.entity.ErrorQuesAttach;
import com.rulin.asp.business.externalservice.uploadapi.entity.AttachCloud;
import com.rulin.asp.business.externalservice.uploadapi.entity.BookErrors;
import com.rulin.asp.business.externalservice.uploadapi.mapper.UploadBookErrorsMapper;
import com.rulin.asp.business.externalservice.util.AspResponse;

@Service
public class UploadBookErrorsService {

	@Autowired
	private UploadBookErrorsMapper uploadBookErrorsMapper;
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	
	@Transactional(rollbackFor=Exception.class)
	public AspResponse uploadBookErrors(List<BookErrors> list) throws Exception {
		AspResponse aspResponse = new AspResponse();
		
		List<Question> questions = new ArrayList<Question>();
		List<ErrorQuesAttach> attachs = new ArrayList<ErrorQuesAttach>();
		List<AttachCloud> pics = new ArrayList<AttachCloud>();
		for (BookErrors bookError : list) {
			String quesId = UUID.randomUUID().toString();//题目id
			Question question = new Question();
			question.setQuesId(quesId);
			question.setBookClass(bookError.getBookClass());
			question.setBookSecClass(bookError.getBookSecClass());
			question.setClaimTime(sdf.parse(bookError.getClaimTime()));
			question.setClaimUserAccount(null);
			question.setClaimUserName(bookError.getClaimUserName());
			question.setMyAnwser(bookError.getMyAnwser());
			question.setPageNum(bookError.getPageNum());
			question.setQuesClosely("0");
			question.setQuesDescribe(bookError.getQuesDescribe());
			question.setQuesDiffLevel(bookError.getQuesDiffLevel());
			question.setQuesTime(sdf.parse(bookError.getQuesTime()));
			question.setQuestionSource(bookError.getQuestionSource());
			question.setQuesType("6");
			String replyId = UUID.randomUUID().toString();//回复ID
			question.setReplyId(replyId);
			question.setReplyTime(sdf.parse(bookError.getClaimTime()));//认领时间和答疑时间相同
//			question.setUserType(bookError.getUserType());
			question.setValid("0");//有效
			question.setTransferSource("1");//来源于览山
			questions.add(question);
			
			ErrorQuesAttach attach = new ErrorQuesAttach();
			attach.setAlipayNum(bookError.getAlipayNum());
			attach.setIsError("1");//是错误
			attach.setIsRepeat("0");//不是重复
			attach.setIsRepeatAdmin("0");//不是重复admin
			attach.setIsReward("1");//是奖励
			attach.setMobilePhone(bookError.getMobilePhone());
			attach.setQuesId(quesId);
			attach.setSubmitterName(bookError.getSubmitterName());
			attachs.add(attach);
			
			List<String> quesPics = bookError.getQuesPics();
			for (String string : quesPics) {
				AttachCloud attachCloud = new AttachCloud();
				String attachId = UUID.randomUUID().toString();
				attachCloud.setAreaId(quesId);
				attachCloud.setAttachAddress(string);
				attachCloud.setAttachId(attachId);
				pics.add(attachCloud);
			}
			List<String> replyPics = bookError.getReplyPics();
			for (String string : replyPics) {
				AttachCloud attachCloud = new AttachCloud();
				String attachId = UUID.randomUUID().toString();
				attachCloud.setAreaId(replyId);
				attachCloud.setAttachAddress(string);
				attachCloud.setAttachId(attachId);
				pics.add(attachCloud);
			}
		}
		try {
			uploadBookErrorsMapper.batchInsertBookErrors(questions);
			uploadBookErrorsMapper.batchInsertBookErrorsAttchs(attachs);
			uploadBookErrorsMapper.batchInsertPics(pics);
		}catch (Exception e) {
			aspResponse.setCode(AspResponse.SERVER_ERROR);
			aspResponse.setMessage(e.getCause().getMessage());
			aspResponse.setErrorMessage(e.getMessage());
			throw new Exception(e);
		}
		aspResponse.setCode(AspResponse.SUCCESS);
		aspResponse.setMessage("OK");
		return aspResponse;
	}
}
